package weifei.wx.community.controller;


import com.auth0.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import weifei.web.Result;
import weifei.wx.community.entity.Tie;
import weifei.wx.community.entity.TieComment;
import weifei.wx.community.entity.TieCommentNotice;
import weifei.wx.community.entity.dto.TieCommentDTO;
import weifei.wx.community.mapper.TieCommentMapper;
import weifei.wx.community.mapper.TieCommentNoticeMapper;
import weifei.wx.community.mapper.TieMapper;
import weifei.wx.community.service.TieCommentService;
import weifei.wx.community.service.UserService;


@RestController
@RequestMapping("/tie/comment")
public class TieCommentController
{
    public static final int PAGE_COUNT = 10;


    @Autowired
    private UserService userService;
    @Autowired
    private TieCommentService tieCommentService;

    @Autowired
    private TieMapper tieMapper;
    @Autowired
    private TieCommentMapper tieCommentMapper;
    @Autowired
    private TieCommentNoticeMapper tieCommentNoticeMapper;


    @PostMapping("/publish")
    public Result publish(HttpServletRequest request, @RequestBody @Valid TieComment comment)
    {
        String token = request.getHeader("access_token");
        Long uid = JWT.decode(token).getClaim("user_id").asLong();

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("id = ?", comment.getTieId());
        Tie tie = this.tieMapper.selectOneByQuery(queryWrapper);
        if (tie == null)
            return Result.failed("不存在的贴子");

        TieComment c = TieComment.create()
                .setTieId(comment.getTieId())
                .setUserId(uid)
                .setContent(comment.getContent());
        if (this.tieCommentMapper.insert(c) == 0)
            return Result.failed("内部错误");

        if (tie.getUserId().equals(uid))
            return Result.success("发表成功");

        // 获取 tieComment.id
        queryWrapper = QueryWrapper.create()
                .select()
                .orderBy("id desc")
                .where("content = ?", comment.getContent());
        c = this.tieCommentMapper.selectOneByQuery(queryWrapper);
        if (c == null)
            return Result.success("发表成功");

        TieCommentNotice notice = new TieCommentNotice();
        notice.setTieUserId(tie.getUserId());
        notice.setTieCommentId(c.getId());
        this.tieCommentNoticeMapper.insert(notice);

        return Result.success("发布成功");
    }


    @RequestMapping("/getTieCommentByID")
    public Result getTieCommentByID(@Valid @NotNull Long id)
    {
        TieCommentDTO commentDTO = this.tieCommentService.getTieCommentByID(id);
        if (commentDTO == null)
            return Result.failed("不存在的评论");
        return Result.success("获取成功", commentDTO);
    }


    @RequestMapping("/getTieCommentByTieID")
    public Result getTieCommentByTieID(@Valid @NotNull Long tieId)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where("tie_id = ?", tieId);
        final var comments = this.tieCommentMapper.selectListByQuery(queryWrapper);
        final var commentsDTO = new TieCommentDTO[comments.size()];

        for(int i = 0; i < comments.size(); i++)
        {
            final Long uid = comments.get(i).getUserId();
            var userDTO = this.userService.getUserByID(uid);
            commentsDTO[i] = new TieCommentDTO(userDTO, comments.get(i));
        }

        return Result.success("获取成功", commentsDTO);
    }


    @RequestMapping("/getTieCommentByPage")
    public Result getTieCommentByPage(
            @Valid @NotNull Long tieId,
            @Valid @NotNull Integer page
        )
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("tie_id = ?", tieId)
                .orderBy("publish_time desc")
                .limit(TieCommentController.PAGE_COUNT * (page - 1), TieCommentController.PAGE_COUNT);
        var comments = this.tieCommentMapper.selectListByQuery(queryWrapper);
        var commentsDTO = new TieCommentDTO[comments.size()];

        for(int i = 0; i < comments.size(); i++)
        {
            final Long uid = comments.get(i).getUserId();
            var userDTO = this.userService.getUserByID(uid);
            commentsDTO[i] = new TieCommentDTO(userDTO, comments.get(i));
        }
        return Result.success("获取成功", commentsDTO);
    }
}



